using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class NetworkGameplayManager : NetworkBehaviour
{
	public PlayersManager playersManager;

	public ScoreManager scoreManager;

	public EnvironmentManager environment;

	public ChatLog chatLog;

	public bool MapEnabled = true;

	[SyncVar]
	[HideInInspector]
	public GameView View;

	[HideInInspector]
	[SyncVar]
	public int PlayersAlive;

	[HideInInspector]
	[SyncVar]
	public int PlayersAliveMax;

	[HideInInspector]
	public int EndgameRanked;

	[HideInInspector]
	[SyncVar]
	public bool IsBattleStart;

	[HideInInspector]
	public bool IsBattleEnded;

	private static int kTargetRpcTargetPlayerKilled;

	private static int kTargetRpcTargetPlayerWin;

	private static int kRpcRpcBroadcastText;

	public GameView NetworkView
	{
		get
		{
			return View;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref View, 1u);
		}
	}

	public int NetworkPlayersAlive
	{
		get
		{
			return PlayersAlive;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref PlayersAlive, 2u);
		}
	}

	public int NetworkPlayersAliveMax
	{
		get
		{
			return PlayersAliveMax;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref PlayersAliveMax, 4u);
		}
	}

	public bool NetworkIsBattleStart
	{
		get
		{
			return IsBattleStart;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref IsBattleStart, 8u);
		}
	}

	public virtual void Awake()
	{
		UnitZ.NetworkGameplay = this;
		UnitZ.gameManager.IsBattleStart = false;
		UnitZ.gameManager.IsPlaying = true;
		if (playersManager == null)
		{
			playersManager = GetComponent<PlayersManager>();
		}
		if (scoreManager == null)
		{
			scoreManager = GetComponent<ScoreManager>();
		}
		if (chatLog == null)
		{
			chatLog = GetComponent<ChatLog>();
		}
	}

	public void PlayerKilled(NetworkConnection target)
	{
		if (base.isServer)
		{
			CallTargetPlayerKilled(target, PlayersAlive + 1);
		}
	}

	[TargetRpc(channel = 0)]
	private void TargetPlayerKilled(NetworkConnection target, int currentRank)
	{
		UnityEngine.Debug.Log("*** You got kill at rank " + currentRank);
		EndgameRanked = currentRank;
		GameOver(isWin: false);
	}

	public void PlayerWin(NetworkConnection target)
	{
		if (base.isServer)
		{
			CallTargetPlayerWin(target);
		}
	}

	[TargetRpc(channel = 0)]
	private void TargetPlayerWin(NetworkConnection target)
	{
		EndgameRanked = 1;
		GameOver(isWin: true);
	}

	[ClientRpc(channel = 0)]
	private void RpcBroadcastText(string text, float sec)
	{
		UnitZ.Hud.BroadcastingText(text, sec);
	}

	public void Broadcast(string text, float sec)
	{
		CallRpcBroadcastText(text, sec);
	}

	public void GameOver(bool isWin)
	{
		if (isWin)
		{
			UnitZ.Hud.OpenPanelByName("Win");
		}
		else
		{
			UnitZ.Hud.OpenPanelByName("Lose");
		}
	}

	public virtual void Start()
	{
		UnitZ.Hud.CountdownText.text = string.Empty;
		UnitZ.Hud.AliveText.text = string.Empty;
		NetworkView = UnitZ.gameManager.GameViewType;
	}

	public virtual void Update()
	{
		if (base.isClient)
		{
			UnitZ.gameManager.GameViewType = View;
		}
		if (UnitZ.Hud.Map != null && !MapEnabled)
		{
			UnitZ.Hud.Map.gameObject.SetActive(value: false);
		}
		if (UnitZ.Hud.MiniMap != null && !MapEnabled)
		{
			UnitZ.Hud.MiniMap.gameObject.SetActive(value: false);
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeRpcRpcBroadcastText(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcBroadcastText called on server.");
		}
		else
		{
			((NetworkGameplayManager)obj).RpcBroadcastText(reader.ReadString(), reader.ReadSingle());
		}
	}

	protected static void InvokeRpcTargetPlayerKilled(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("TargetRPC TargetPlayerKilled called on server.");
		}
		else
		{
			((NetworkGameplayManager)obj).TargetPlayerKilled(ClientScene.readyConnection, (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeRpcTargetPlayerWin(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("TargetRPC TargetPlayerWin called on server.");
		}
		else
		{
			((NetworkGameplayManager)obj).TargetPlayerWin(ClientScene.readyConnection);
		}
	}

	public void CallRpcBroadcastText(string text, float sec)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcBroadcastText called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcBroadcastText);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(text);
		networkWriter.Write(sec);
		SendRPCInternal(networkWriter, 0, "RpcBroadcastText");
	}

	public void CallTargetPlayerKilled(NetworkConnection target, int currentRank)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("TargetRPC Function TargetPlayerKilled called on client.");
			return;
		}
		if (target.connectionId == 0)
		{
			UnityEngine.Debug.LogError("TargetRPC Function TargetPlayerKilled called on connection to server");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kTargetRpcTargetPlayerKilled);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)currentRank);
		SendTargetRPCInternal(target, networkWriter, 0, "TargetPlayerKilled");
	}

	public void CallTargetPlayerWin(NetworkConnection target)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("TargetRPC Function TargetPlayerWin called on client.");
			return;
		}
		//if (target.connectionId == 0)
		//{
		//	UnityEngine.Debug.LogError("TargetRPC Function TargetPlayerWin called on connection to server");
		//	return;
		//}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kTargetRpcTargetPlayerWin);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendTargetRPCInternal(target, networkWriter, 0, "TargetPlayerWin");
	}

	static NetworkGameplayManager()
	{
		kRpcRpcBroadcastText = -1884729601;
		NetworkBehaviour.RegisterRpcDelegate(typeof(NetworkGameplayManager), kRpcRpcBroadcastText, InvokeRpcRpcBroadcastText);
		kTargetRpcTargetPlayerKilled = 1261560566;
		NetworkBehaviour.RegisterRpcDelegate(typeof(NetworkGameplayManager), kTargetRpcTargetPlayerKilled, InvokeRpcTargetPlayerKilled);
		kTargetRpcTargetPlayerWin = 1304071171;
		NetworkBehaviour.RegisterRpcDelegate(typeof(NetworkGameplayManager), kTargetRpcTargetPlayerWin, InvokeRpcTargetPlayerWin);
		NetworkCRC.RegisterBehaviour("NetworkGameplayManager", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write((int)View);
			writer.WritePackedUInt32((uint)PlayersAlive);
			writer.WritePackedUInt32((uint)PlayersAliveMax);
			writer.Write(IsBattleStart);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write((int)View);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32((uint)PlayersAlive);
		}
		if ((base.syncVarDirtyBits & 4) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32((uint)PlayersAliveMax);
		}
		if ((base.syncVarDirtyBits & 8) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(IsBattleStart);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			View = (GameView)reader.ReadInt32();
			PlayersAlive = (int)reader.ReadPackedUInt32();
			PlayersAliveMax = (int)reader.ReadPackedUInt32();
			IsBattleStart = reader.ReadBoolean();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			View = (GameView)reader.ReadInt32();
		}
		if ((num & 2) != 0)
		{
			PlayersAlive = (int)reader.ReadPackedUInt32();
		}
		if ((num & 4) != 0)
		{
			PlayersAliveMax = (int)reader.ReadPackedUInt32();
		}
		if ((num & 8) != 0)
		{
			IsBattleStart = reader.ReadBoolean();
		}
	}
}
